import { useEffect, useState } from 'react';
import {
  getMessageGroup,
  getMessageDetail,
  getMessageCount,
  getMessage,
  getMessageAllGroup,
  getAppMessge,
} from './service';
import { useForm } from '@formily/react';
import { inbizMessage } from '@inbiz/utils';

// 获取有消息得分组
export const useGetMessageGroupEffect = () => {
  const form = useForm();
  const { appInfo } = form as unknown as { appInfo: { appId: string } };
  const requestServer = async (group: any, callback: Function) => {
    let res = await getMessageGroup({
      siteOutId: appInfo?.SiteInfo?.OutId,
      dataVersionId: appInfo?.SiteInfo?.DataVersionId,
      appId: appInfo?.AppId,
      group: group,
    });

    // let res2 = await getMessageCount({
    //   siteOutId: appInfo?.SiteInfo?.OutId,
    //   dataVersionId: appInfo?.SiteInfo?.DataVersionId,
    //   appId: appInfo?.AppId,
    //   group: group,
    // });
    if (!res) {
      // inbizMessage('消息获取失败');
      return;
    }
    if (callback) callback(res);
  };

  return requestServer;
};

// 获取所有分组
export const useGetMessageGroupAllEffect = () => {
  const requestServer = async ({
    appId,
    searchText,
    pageIndex,
    pageSize,
    callback,
  }: {
    appId: string;
    searchText?: string;
    pageIndex?: number;
    pageSize?: number;
    callback?: Function;
  }) => {
    let res = await getMessageAllGroup({
      appId: appId,
      isOtherSites: appId == 'other' ? true : false,
      searchText: searchText ? searchText : '',
      pageIndex: pageIndex ? pageIndex : 1,
      pageSize: pageSize ? pageSize : 10,
    });
    if (!res) {
      inbizMessage('获取消息分组失败');
      return;
    }
    return res;
  };

  return requestServer;
};

// 树形消息
export const useGetMessageAllTreeGroupEffect = () => {
  const [data, $data] = useState(null);
  const requestServer = async () => {
    let res = await getAppMessge({});
    if (!res) {
      return;
    }
    $data(res);
  };

  useEffect(() => {
    requestServer();
  }, []);

  if (!data) {
    return false;
  }
  return data;
};

export const useGetMessageDetailEffect = () => {
  const form = useForm();
  const { appInfo } = form as unknown as { appInfo: { appId: string } };
  const requestServer = async ({
    outId,
    callback,
  }: {
    outId: string;
    callback?: Function;
  }) => {
    await getMessageDetail({
      siteOutId: appInfo?.SiteInfo?.OutId,
      dataVersionId: appInfo?.SiteInfo?.DataVersionId,
      appId: appInfo?.AppId,
      id: outId,
      pageIndex: 1,
      pageSize: 20,
    });
  };
  return requestServer;
};

export const useGetMessageCountEffect = () => {
  const form = useForm();
  const { appInfo } = form as unknown as { appInfo: { appId: string } };
  const requestServer = async ({
    outId,
    callback,
  }: {
    outId: string;
    callback?: Function;
  }) => {
    await getMessageCount({
      siteOutId: appInfo?.SiteInfo?.OutId,
      dataVersionId: appInfo?.SiteInfo?.DataVersionId,
      appId: appInfo?.AppId,
    });
  };
  return requestServer;
};

export const useGetMessageEffect = () => {
  const requestServer = async ({
    group,
    pageIndex = 1,
    allGroup,
    callback,
    pageNumber,
    readState,
  }: {
    group?: any;
    pageIndex?: number;
    pageNumber?: number;
    allGroup: any;
    callback?: Function;
    readState: number;
  }) => {
    let res = await getMessage({
      // siteOutId: appInfo?.SiteInfo?.OutId,
      // dataVersionId: appInfo?.SiteInfo?.DataVersionId,
      // appId: appInfo?.AppId,
      pageIndex: pageIndex,
      pageSize: pageNumber,
      readState: readState,
      group: allGroup
        ? allGroup
        : [
          {
            groupKey: group.GroupKey,
            appId: group.AppId,
          },
        ],
    });
    if (res) {
      callback && callback(res);
    }
  };
  return requestServer;
};
